package com.yksj.healthtalk.ui.doctorstation;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.universalimageloader.core.DisplayImageOptions;
import org.universalimageloader.core.ImageLoader;
import org.universalimageloader.core.display.RoundedBitmapDisplayer;
import org.universalimageloader.utils.StorageUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.DoctorPatientListEntity;
import com.yksj.healthtalk.entity.UrlEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.doctorstation.service.DoctorAttentionMyListActivity;
import com.yksj.healthtalk.ui.doctorstation.service.DoctorServiceGroupMenberActivity;
import com.yksj.healthtalk.ui.friend.FriendSearchMainActivity;
/**
 * 我的患者 列表
 * @author Administrator
 *
 */
public class PatientServiceMainFragmentList extends BaseFragmentActivity implements OnClickListener, OnChildClickListener{
	
	private DoctorServicePatientListAdapter madapter;
	List<DoctorPatientListEntity> parseToList;
	private String mData;
	private ExpandableListView	mListView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(savedInstanceState!=null){
			mData=savedInstanceState.getString("data");	
		}
		
		setContentView(R.layout.doctor_service_fragment_listview);
		
		initView();
	}

	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("患者管理");
		findViewById(R.id.top_line).setVisibility(View.GONE);
		findViewById(R.id.listview_fragment).setVisibility(View.GONE);
		mListView = (ExpandableListView) findViewById(R.id.expandableListView);
		mListView.setVisibility(View.VISIBLE);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setText("搜索");
		madapter=new DoctorServicePatientListAdapter(this);
		mListView.setCacheColorHint(Color.TRANSPARENT);
		mListView.setAdapter(madapter);
		mListView.setGroupIndicator(null);
		mListView.setOnChildClickListener(this);
	}

	
	private void onParseTo(String content){
			mData=content;
		if(content==null||"".equals(content)){
			
		}else{
			parseToList = DoctorPatientListEntity.parseToList(content);
			if(parseToList!=null)
				madapter.onChange(parseToList);
		}
		
		for(int i = 0; i < 2; i++){  
			mListView.expandGroup(i);  
		} 
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("data", mData);
	}
	
	private void initData() {
		HttpRestClient.doHttpFindMyPatientList(SmartFoxClient.getLoginUserId(),new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				onParseTo(content);
			}
		});
	}
	
	class DoctorServicePatientListAdapter extends BaseExpandableListAdapter{
		private List<DoctorPatientListEntity> list=new ArrayList<DoctorPatientListEntity>();
		private LayoutInflater from;
		private Context context;
		private ImageLoader mImageLoader;
		private DisplayImageOptions mOptions;
		
		public DoctorServicePatientListAdapter(Context context) {
			from = LayoutInflater.from(context);
			this.context=context;
			mImageLoader = ImageLoader.getInstance();
			mOptions = createUnknowHeaderDisplayImageOptions(context);
		}

		public void onChange(List<DoctorPatientListEntity> parseToList) {
			list.clear();
			this.list.addAll(parseToList);
			notifyDataSetChanged();
		}

		DisplayImageOptions createUnknowHeaderDisplayImageOptions(Context context){
			DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder(context)
			.showStubImage(R.drawable.default_jiugong_icon)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.cacheOnDisc(new File(StorageUtils.getHeadersPath()))
			.cacheInMemory()
			.displayer(new RoundedBitmapDisplayer(5))
			.build();
			return displayImageOptions;
		}
		
		@Override
		public int getGroupCount() {
			return 2;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			if(groupPosition==0){
				return 2;
			}else{
				return list.size()-2;
			}
		}

		@Override
		public Object getGroup(int groupPosition) {
			return "服务中的患者";
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return list.get(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			convertView = from.inflate(R.layout.knowledge_detail_expand_group, null);
			TextView name=(TextView)convertView.findViewById(R.id.expand_group_text);
			ImageView imageView=(ImageView) convertView.findViewById(R.id.expand_group_head);
			name.setText("服务中的患者");
			
		      if(groupPosition==0){
		    	 TextView textView=new TextView(context);
		    	 textView.setHeight(0);
		    	  return textView;
		      }else{
		    	  if(isExpanded){
			    	  imageView.setBackgroundResource(R.drawable.friend_group_arrows_down);
		            }else{
		            	imageView.setBackgroundResource(R.drawable.friend_group_arrows);
		            }
		    	  return convertView;  
		      }
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			
			if(convertView==null||convertView.getTag()==null){
				convertView=from.inflate(R.layout.doctor_service_all_listview_item, null);
			}
			TextView name=(TextView) convertView.findViewById(R.id.name);
			GridView view= (GridView) convertView.findViewById(R.id.image);
			
			DoctorPatientListEntity entity;
			if(groupPosition!=0){
				entity = list.get(childPosition+2);
			}else{
				entity = list.get(childPosition);
			}
			name.setText(entity.getName()+"("+entity.getNum()+")");
			view.setAdapter(new myGrived(entity.getUrlEntities()));
//			mImageLoader.displayImage(entity.getUrlEntities(),  view, mOptions);
			return convertView;
		}
		
		@Override
		public boolean hasStableIds() {
			return true;
		}
		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

	}
	
	class myGrived extends BaseAdapter{
		private  List<UrlEntity> urlEntities=new ArrayList<UrlEntity>();
		private ImageLoader instance;
		public myGrived(List<UrlEntity> urlEntities) {
			instance = ImageLoader.getInstance();
			this.urlEntities.clear();
			if(urlEntities!=null)
			this.urlEntities.addAll(urlEntities);
		}

		@Override
		public int getCount() {
			return urlEntities.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}
		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView=new ImageView(getApplicationContext());
			imageView.setLayoutParams(new AbsListView.LayoutParams(35,35));
			instance.displayImage(urlEntities.get(position).getSex(), urlEntities.get(position).getUrl(), imageView);
			return imageView;
		}
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2:
			Intent intent=new Intent(this,FriendSearchMainActivity.class);
			startActivity(intent);
			break;
		}
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		DoctorPatientListEntity entity;
		
		if(groupPosition!=0){
			entity=parseToList.get(childPosition+2);
		}else{
			entity=parseToList.get(childPosition);
		}
		Intent intent;
		if("3".equals(entity.getId())){//历史
			intent=new Intent(PatientServiceMainFragmentList.this,DoctorSettingClientHistoryActivity.class);
		}else if("5".equals(entity.getId())){//我的患者
			intent = new Intent(this,DoctorServiceGroupMenberActivity.class);
		}else if("6".equals(entity.getId())){//关注我的
			intent = new Intent(this,DoctorAttentionMyListActivity.class);
		}else{
			intent=new Intent(this,DoctorPatientMainListActivity.class);
			intent.putExtra("parame", entity);
		}
		startActivity(intent);
		return true;
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		initData();
	}
}
